<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\Request;
use think\facade\Db;

class Admin extends Base
{
    protected $middleware = [
        '\app\admin\middleware\LoginCheck',
        '\app\admin\middleware\UrlData',
        '\app\admin\middleware\UrlCheck'

    ];

    //管理员控制器
    public function index() //管理员列表
    {

        return view('');
    }

    public function data($limit,$page)  //管理员表数据
    {
        $offset = ($page-1)*$limit;
        $limit = (int)$limit;

        $list = Db::table('admin')
            ->alias('a')
            ->leftJoin('admin_role a_r','a.id=a_r.admin_id')
            ->leftJoin('role r','r.id=a_r.role_id')
            ->where('status','<>',2)
            ->limit($offset,$limit)
            ->field('a.id,a.admin_name,a.admin_pwd,a.status,a.create_time,a.update_time,group_concat(r.role_name) role')
            ->group('a.id')
            ->select()->toArray();


        $count = Db::table('admin')->where('status','<>',2)->count();

        foreach ($list as $k=>$v){
            if(empty($list[$k]['role'])){
                $list[$k]['role']='暂无任何角色';
            }
        }

        return json([
            "code"=>0,
            "msg"=>"",
            "count"=>$count,
            "data"=>$list
        ]);
    }

    public function update()    //管理员的修改
    {
        if($this->request->isPost()){   //用户提交修改信息
            $data = $this->request->post();

            if(isset($data['role_id'])){
                $role_id = $data['role_id'];
                unset($data['role_id']);
            }


            $user = Db::table('admin')
                ->where('admin_name',$data['admin_name'])
                ->where('id','<>',$data['id'])
                ->select()
                ->toArray();

            if(!empty($user)){
                $this->error('已有相同的账号');
            }

            if(empty($data['admin_pwd'])){  //如果没修改密码
                unset($data['admin_pwd']);
            }else{
                $data['admin_pwd'] = md5($data['admin_pwd']);
            }

            $data['update_time'] = date("Y-m-d H:i:s");

            $res = Db::table('admin')->update($data);

            if($res){
                $del = Db::table('admin_role')->where('admin_id',$data['id'])->delete();
                if(isset($role_id)){
                    $save = $this->makeNode($role_id,$data['id']);
                    $res = Db::table('admin_role')->insertAll($save);
                    if($res){
                        $this->success('管理员修改成功','index');
                    }
                }else{
                    $this->success('管理员修改成功','index');
                }


            }
            $this->error('管理员修改失败');

        }
        $role = Db::table('role')->field('id,role_name')->select();

        $id = $this->request->get('id');    //获取修改用户id
        $user = Db::table('admin')->where('id',$id)->field('id,admin_name,admin_pwd,status')->find();
        $a_r = Db::table('admin_role')->where('admin_id',$id)->select()->toArray();

        $role_have = [];
        foreach ($a_r as $v){
            $role_have[] = $v['role_id'];
        }



        return view('add',['user'=>$user,'role'=>$role,'a_r'=>$role_have]);
    }

    public function add()   //管理员添加
    {
        if($this->request->isPost()){
            $data = $this->request->post();
            if(isset($data['role_id'])){
                $role_id = $data['role_id'];
                unset($data['role_id']);
            }


            $data = $this->insertTime($data);


            $user = Db::table('admin')->where('admin_name',$data['admin_name'])->select()->toArray();

            if(!empty($user)){
                $this->error('已有相同的账号');
            }
            $data['admin_pwd'] = md5($data['admin_pwd']);


            $id = Db::table('admin')->insertGetId($data);
            if($id){
                if(isset($role_id)){
                    $save = $this->makeNode($role_id,$id);
                    $res = Db::table('admin_role')->insertAll($save);
                    if($res){
                        $this->success('管理员添加成功','index');
                    }
                }else{
                    $this->success('管理员添加成功','index');
                }
            }

            $this->error('管理员添加失败');
        }

        $role = Db::table('role')->field('id,role_name')->select();


        return view('',['role'=>$role]);
    }

    public function delete()    //管理员删除
    {
        if($this->request->isPost()){
            $id = $this->request->post('id');

            $res = Db::table('admin')->where('id',$id)->update(['status' => '2']);

            if($res){
                return json([
                    'code'=>200,
                    'msg'=>'删除成功'
                ]);
            }
            return json([
                'code'=>400,
                'msg'=>'删除失败'
            ]);
        }
    }

    private function makeNode($role,$id)    //
    {
        $role_id = [];


        foreach ($role as $v){   //对 // 这种节点id进行处理
            $role_id = array_merge($role_id,explode('/',$v));
        }
        $node_id = array_unique($role_id);  //给节点元素去重

        $save = [];
        foreach ($role_id as $v){
            $save[] = ['admin_id'=>$id,'role_id'=>$v,'create_time'=>date("Y-m-d H:i:s"),'update_time'=>date("Y-m-d H:i:s")];
        }

        return $save;
    }




}
